﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Caching;
using WebEntities;
using com.subb.WebInformation;

namespace WebsiteUI.Extensions
{
    public class CommonFn
    {
        /// <summary>
        /// 从web.config获取栏目ID属性
        /// </summary>
        /// <param name="idName">ID名称</param>
        /// <returns></returns>
        public static int GetContentID(string idName)
        {
            int cataId;
            if (!int.TryParse(ConfigurationManager.AppSettings[idName], out cataId))
            {
                cataId = 0;
            }
            return cataId;
        }

        /// <summary>
        /// 缓存数据。
        /// </summary>
        /// <param name="cacheName">缓存数据的名称</param>
        /// <param name="data">缓存的数据</param>
        /// <param name="expSecs">有效时间（秒）</param>
        public static void CacheData(string cacheName, object data, int expSecs)
        {
            //int expSecs = GetContentID("CacheExpiration");
            expSecs = Math.Abs(expSecs);
            HttpRuntime.Cache.Add(cacheName, data, null, DateTime.Now.AddSeconds(expSecs), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }

        public static object GetCacheData(string cacheName)
        {
            return HttpRuntime.Cache[cacheName];
        }

        public static List<WI_News> GetNews(string idName, int start, int limit, out int ttlCount, string query)
        {
            int cataId = GetContentID(idName);
            List<WI_News> newsList = new List<WI_News>();
            ttlCount = 0;
            WI_NewsDAL ndal = new WI_NewsDAL();
            newsList = ndal.GetAllNews(cataId, start, limit, out ttlCount, query);
            return newsList;
        }

        /// <summary>
        /// 获取新闻分页（预编译查询）
        /// </summary>
        public static List<WI_News> GetNews(string idName, int start, int limit)
        {
            int cataId = GetContentID(idName);
            List<WI_News> newsList = new List<WI_News>();
            WI_NewsDAL ndal = new WI_NewsDAL();
            newsList = ndal.GetAllNews(cataId, start, limit);
            return newsList;
        }

        public static List<WI_News> GetNews(string idName, string cacheName, int start, int limit, out int ttlCount, string query, int expSecs)
        {
            int cataId = GetContentID(idName);
            List<WI_News> newsList = new List<WI_News>();
            object objData = CommonFn.GetCacheData(cacheName);
            ttlCount = 0;
            if (objData == null)
            {
                WI_NewsDAL ndal = new WI_NewsDAL();
                newsList = ndal.GetAllNews(cataId, start, limit, out ttlCount, query);
                if (newsList.Count > 0)
                {
                    CommonFn.CacheData(cacheName, newsList, expSecs);
                }
            }
            else
            {
                newsList = objData as List<WI_News>;
            }
            return newsList;
        }

        /// <summary>
        /// 获取新闻分页（预编译查询）
        /// </summary>
        public static List<WI_News> GetNews(string idName, string cacheName, int start, int limit, int expSecs)
        {
            int cataId = GetContentID(idName);
            List<WI_News> newsList = new List<WI_News>();
            object objData = CommonFn.GetCacheData(cacheName);
            if (objData == null)
            {
                WI_NewsDAL ndal = new WI_NewsDAL();
                newsList = ndal.GetAllNews(cataId, start, limit);
                if (newsList.Count > 0)
                {
                    CommonFn.CacheData(cacheName, newsList, expSecs);
                }
            }
            else
            {
                newsList = objData as List<WI_News>;
            }
            return newsList;
        }

        /// <summary>
        /// 获取新闻分页（预编译查询）
        /// </summary>
        public static List<WI_News> GetNews(int cataId, string cacheName, int start, int limit, int expSecs)
        {
            List<WI_News> newsList = new List<WI_News>();
            object objData = CommonFn.GetCacheData(cacheName);
            if (objData == null)
            {
                WI_NewsDAL ndal = new WI_NewsDAL();
                newsList = ndal.GetAllNews(cataId, start, limit);
                if (newsList.Count > 0)
                {
                    CommonFn.CacheData(cacheName, newsList, expSecs);
                }
            }
            else
            {
                newsList = objData as List<WI_News>;
            }
            return newsList;
        }

        public static List<WI_NewsCata> GetNewsCata(int pcataId, string cacheName, int start, int limit, int expSecs)
        {
            List<WI_NewsCata> cataList = new List<WI_NewsCata>();
            object objData = CommonFn.GetCacheData(cacheName);
            if (objData == null)
            {
                WI_NewsCataDAL ncdal = new WI_NewsCataDAL();
                cataList = ncdal.GetAllNewsCataByPcataId(pcataId, start, limit);
                if (cataList.Count > 0)
                {
                    CommonFn.CacheData(cacheName, cataList, expSecs);
                }
            }
            else
            {
                cataList = objData as List<WI_NewsCata>;
            }
            return cataList;
        }
    }
}